/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */

package storage.dbhelper;

import cn.edu.thu.log.web.service.PropertiesReaderServices;
import cn.edu.thu.log.web.service.impl.PropertiesReaderServicesImpl;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.KeySlice;
import storage.dbobject.DbProduct;
import storage.util.StorageManager;

/**
 * 
 * @author fenyo rome
 *
 */
public class ProductHelper {

	/**
	 * 将product保存到LaUDMS数据库中
	 * 
	 * @param product
	 *            要存储的模型对象
	 */
	public void saveProduct(DbProduct product) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_product"));
		// sm.open();
		sm.setRowKey(product.getRowKey());
		sm.put(DbProduct.CL_NAME, product.getName());
		sm.put(DbProduct.CL_UPDATETIME, product.getUpdateTime());
		sm.put(DbProduct.CL_CHINESE, product.getChinese());
		ArrayList<String> logBodyList = product.getLogBodyList();
		for (int i = 0; i < logBodyList.size(); i++) {
			sm.put(DbProduct.CL_LOGBODY + "_" + logBodyList.get(i),
					logBodyList.get(i));
		}
		// sm.close();

	}

	/**
	 * 将product保存到LaUDMS数据库中
	 * 
	 * @param product
	 *            要存储的模型对象
	 */
	public void updateProduct(DbProduct product) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_product"));
		// sm.open();
		sm.delete(product.getRowKey());
		sm.setRowKey(product.getRowKey());
		sm.put(DbProduct.CL_NAME, product.getName());
		sm.put(DbProduct.CL_UPDATETIME, product.getUpdateTime());
		if (product.getChinese() == null) {
			product.setChinese("");
		}
		sm.put(DbProduct.CL_CHINESE, product.getChinese());
		ArrayList<String> logBodyList = product.getLogBodyList();
		for (int i = 0; i < logBodyList.size(); i++) {
			sm.put(DbProduct.CL_LOGBODY + "_" + logBodyList.get(i),
					logBodyList.get(i));
		}
		// sm.close();
	}

	/**
	 * 将一系列logHead保存到LaUDMS数据库中
	 * 
	 * @param products
	 *            要存储的模型对象列表
	 */
	public void saveProducts(List<DbProduct> products) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_product"));
		// sm.open();
		for (int i = 0; i < products.size(); i++) {
			DbProduct product = products.get(i);
			sm.setRowKey(product.getRowKey());
			sm.put(DbProduct.CL_NAME, product.getName());
			sm.put(DbProduct.CL_UPDATETIME, product.getUpdateTime());
			sm.put(DbProduct.CL_CHINESE, product.getChinese());
			ArrayList<String> logBodyList = product.getLogBodyList();
			for (int j = 0; j < logBodyList.size(); j++) {
				sm.put(DbProduct.CL_LOGBODY + "_" + logBodyList.get(i),
						logBodyList.get(j));
			}
		}
		// sm.close();
	}
/**
 * 
 * @param columnName
 * columnName
 * @param value
 * value
 * @return
 */
	public List<DbProduct> getProducts(final String columnName,
			final String value) {
		List<DbProduct> result = new LinkedList<DbProduct>();
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_product"));
		// sm.open();
		List<KeySlice> crs = sm.get(columnName, value);
		try {
			for (KeySlice ks : crs) {
				String rowKey = new String(ks.getKey());
				String name = null, chinese = null, updateTime = null;
				ArrayList<String> logBodyList = new ArrayList();
				for (ColumnOrSuperColumn column : ks.getColumns()) {
					String columnsName = new String(column.getColumn()
							.getName());
					String columnsValue = new String(column.getColumn()
							.getValue());
					if (columnsName.equals(DbProduct.CL_CHINESE)) {
						chinese = columnsValue;
					} else if (columnsName.equals(DbProduct.CL_NAME)) {
						name = columnsValue;
					} else if (columnsName.equals(DbProduct.CL_UPDATETIME)) {
						updateTime = columnsValue;
					} else if (columnsName.startsWith(DbProduct.CL_LOGBODY)) {
						logBodyList.add(columnsValue);
					}
				}
				DbProduct pProduct = new DbProduct(rowKey, name, chinese,
						updateTime, logBodyList);
				result.add(pProduct);
			}
		} catch (Exception ex) {
			Logger.getLogger(DbProduct.class.getName()).log(Level.SEVERE, null,
					ex);
		}
		return result;
	}
	/**
	 * 
	 * @return ArrayList<String>
	 */
	public ArrayList<String> getAllProductName() {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_product"));
		// sm.open();
		List<KeySlice> crs = sm.getAll();
		ArrayList<String> productNameList = new ArrayList<String>();
		try {
			for (KeySlice ks : crs) {
				String name = "";
				for (ColumnOrSuperColumn column : ks.getColumns()) {
					String columnsName = new String(column.getColumn()
							.getName());
					String columnsValue = new String(column.getColumn()
							.getValue());
					if (columnsName.equals(DbProduct.CL_NAME)) {
						name = columnsValue;
					}
				}
				if (!productNameList.contains(name) && name.length() > 0) {
					productNameList.add(name);
				}
			}
		} catch (Exception ex) {
			Logger.getLogger(DbProduct.class.getName()).log(Level.SEVERE, null,
					ex);
		} 
		return productNameList;
	}
/**
 * 
 * @param rowKey
 * rowKey
 */
	public void deleteProduct(final String rowKey) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_product"));
		// sm.open();
		sm.delete(rowKey);
		// sm.close();
	}

	/**
	 * 为产品添加日志体字段
	 * 
	 * @param product
	 *            产品
	 * @param logBody
	 *            日志体字段
	 */
	public void addLogBody(DbProduct product, String logBody) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_product"));
		// sm.open();
		sm.setRowKey(product.getRowKey());
		sm.put(DbProduct.CL_LOGBODY + "_" + logBody, logBody);
		// sm.close();

	}

	/**
	 * 
	 * @param productName
	 * productName
	 * @param logBodyname
	 * logBodyname
	 */
	public void deleteLogBody(String productName, String logBodyname) {
		throw new UnsupportedOperationException("Not yet implemented");
	}
}
